import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor

df = pd.DataFrame({'height': [1.56, 1.52, 1.78, 1.67, 1.90, 1.84],
                   'age': [23, 20, 16, 18, 45, 24],
                   'weight': [120, 150, 105, 90, 169, 110],
                   "BMI": [3.9, 4.0, 4.4, np.nan, np.nan, np.nan]})
df_x = df.drop(['BMI'], axis=1)  # 不要BMI列
df_y = df.loc[:, 'BMI']         # 取出BMI列
y_train = df_y[df_y.notnull()]  # 取出非空
y_test = df_y[df_y.isnull()]  # 取出空的待计算

print('y_train：', y_train, type(y_train))
print('y_train.index：', y_train.index)
input()
x_train = df_x.iloc[y_train.index]
x_test = df_x.iloc[y_test.index]

rfc = RandomForestRegressor(n_estimators=100)
rfc = rfc.fit(x_train, y_train)
ypred = rfc.predict(x_test)

print(df_y.isnull())
print(df_y)
df_y[df_y.isnull()] = ypred
print(df_y)

print(df)